package com.jack.hiltsimple.mvvm.view

import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import com.jack.hiltsimple.R
import com.jack.hiltsimple.inter_bind.DiInterface
import com.jack.hiltsimple.inter_provide.DiInterface2
import com.jack.hiltsimple.inter_provide_qualifier.DiInterface3
import com.jack.hiltsimple.inter_provide_qualifier.DiInterfaceImplModule1
import com.jack.hiltsimple.mvvm.vm.BannerViewModel
import com.jack.hiltsimple.normal.Truck
import com.jack.hiltsimple.third_sdk.ApiResponse
import com.jack.hiltsimple.third_sdk.ApiService
import com.jack.hiltsimple.third_sdk.BanInfos
import dagger.hilt.android.AndroidEntryPoint
import jakarta.inject.Inject
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

    val TAG = "Hilt的使用 "

    @Inject
    lateinit var truck: Truck

    @Inject
    lateinit var mDiInterface: DiInterface

    @Inject
    lateinit var mDiInterface2: DiInterface2

    @DiInterfaceImplModule1.diInterfaceImpl31
    @Inject
    lateinit var mDiInterface3: DiInterface3

    // 直接注入 ApiService！Hilt 会从 NetworkModule 中提供实例
    @Inject
    lateinit var apiService: ApiService

    // Hilt 会自动创建 BannerViewModel 并提供所需的 BannerRepository
    private val viewModel: BannerViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        truck.deliver()//基础用法
        mDiInterface.method()//module + binds
        mDiInterface2.method()//module + Provides
        mDiInterface3.method()//module + Provides + Qualifier

        //不使用hilt
//        val retrofit = Retrofit.Builder().baseUrl("https://www.wanandroid.com/")
//            .addConverterFactory(GsonConverterFactory.create())
//            .build()
//        val request = retrofit.create<ApiService>(ApiService::class.java)
//        val call: Call<ApiResponse<MutableList<BanInfos>>> = request.bannerInfos()

        //第三方sdk 使用hilt
        val call: Call<ApiResponse<MutableList<BanInfos>>> = apiService.bannerInfos()
        call.enqueue(object : Callback<ApiResponse<MutableList<BanInfos>>> {
            override fun onResponse(
                call: Call<ApiResponse<MutableList<BanInfos>>>,
                response: Response<ApiResponse<MutableList<BanInfos>>>
            ) {
                Log.d(TAG, "onResponse aa1: " + response.body())
            }

            override fun onFailure(
                call: Call<ApiResponse<MutableList<BanInfos>>>,
                t: Throwable
            ) {
                Log.d(TAG, "onResponse aa2: " + t.message)
            }
        })

        viewModel.banner.observe(this,object : Observer<MutableList<BanInfos>> {
            override fun onChanged(value: MutableList<BanInfos>) {
                Log.d(TAG, "onResponse 来自VM层: $value")
            }
        })

        viewModel.loadBanner()
    }
}